package cn.edu.ctbu.demo1.webapi;

import cn.edu.ctbu.demo1.core.PageUtils;
import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Sc;
import cn.edu.ctbu.demo1.domain.Tc;
import cn.edu.ctbu.demo1.service.ScService;
import cn.edu.ctbu.demo1.service.TcService;
import cn.edu.ctbu.demo1.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/webapi/sc")
public class ScRestController {
    @Autowired
    ScService scService;
    @Autowired
    TcService tcService;
    /**
     * 读取的url:/webapi/sc/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        Sc sc = scService.findById(id);
        return RUtil.success(sc);
    }

    /**
     * 新增学生的方法
     * @param sc
     * @return
     */
    @PostMapping("/insert")
    public R insert(Sc sc){

        Sc sc1 = scService.insert(sc);
        return RUtil.success(sc1);
    }

    /**
     * 更新学生的方法
     * @param sc
     * @return
     */
    @PutMapping("/update")
    public R update(Sc sc){
        scService.update(sc);
        return RUtil.success(sc);
    }
    /**
     * 删除学生的方法
     * @param id
     * @deturn
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        scService.delete(id);
        return RUtil.success(null);
    }

    /**
     * 分页,读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(
            @RequestParam(value = "page",defaultValue ="0")Integer page,
            @RequestParam(value = "size",defaultValue ="10")Integer size,
            @RequestParam(value ="content",defaultValue = "")String content
    ){
        Page<Sc> scPage;
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){
            Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.ASC,"id"));
            scPage = scService.findAll(pageable);
        }
        else {
            Sc sc = new Sc();
            Pageable pageable = PageRequest.of(0,10, Sort.by(Sort.Direction.ASC,"id"));
            ExampleMatcher matcher = null;
            sc.setCname(content);
            matcher = ExampleMatcher.matching()//模糊查询
                    .withMatcher("cname", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Sc> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);
            }
        PageUtils pageUtils = new PageUtils(scPage.getContent(),scPage.getTotalElements());
        return RUtil.success(pageUtils);
    }

    /**
     * 获取成绩信息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/get/score")
    public R getScore(HttpServletRequest httpServletRequest,
                      @RequestParam(value = "page",defaultValue ="0")Integer page,
                      @RequestParam(value = "size",defaultValue ="10")Integer size,
                      @RequestParam(value ="select",defaultValue = "")Integer select,
                      @RequestParam(value ="content",defaultValue = "")String content){

//        return RUtil.success(sc);
        Page<Sc> scPage = null;
        Sc sc =new Sc();
        HttpSession session = httpServletRequest.getSession();
        String sno = (String) session.getAttribute("studentNo");
        Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.ASC,"id"));
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){

            sc.setSno(sno);
            ExampleMatcher matcher = ExampleMatcher.matching().
                    withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Sc> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);

        }else {
            sc.setCname(content);
            sc.setSno(sno);
            ExampleMatcher matcher = ExampleMatcher.matching().
                    withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("sno",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Sc> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(scPage.getContent(),scPage.getTotalElements());
        return RUtil.success(pageUtils);

    }

    /**
     * 查询课程信息
     * @param httpServletRequest
     * @param page
     * @param size
     * @param select
     * @param content
     * @return
     */
    @GetMapping("/get/lesson")
    public R getLesson (HttpServletRequest httpServletRequest,
                        @RequestParam(value = "page",defaultValue ="0")Integer page,
                        @RequestParam(value = "size",defaultValue ="10")Integer size,
                        @RequestParam(value ="select",defaultValue = "")Integer select,
                        @RequestParam(value ="content",defaultValue = "")String content){
        /**
         * 获取教师姓名
         */
        HttpSession session = httpServletRequest.getSession();
        String name = (String) session.getAttribute("teacherName");
        /**
         * 通过教师姓名来查找出TC表的No
         */
        Tc tc = new Tc();
        tc = tcService.findByTname(name);
        String tno = tc.getNo();
        /**
         * 通过Tc表中的no来查找每个老师所交的门课信息和所有学生信息
         */
        Page<Sc> scPage = null;
        Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.ASC,"sno"));
        Sc sc = new Sc();

        if(StringUtils.isEmpty((content))){

            sc.setTno(tno);
            ExampleMatcher matcher = ExampleMatcher.matching().
                    withMatcher("tno",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Sc> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);

        } else {
            if (select.equals(1)) {
                sc.setSno(content);
                ExampleMatcher matcher = ExampleMatcher.matching().
                        withMatcher("sno", ExampleMatcher.GenericPropertyMatchers.contains());
                Example<Sc> example = Example.of(sc, matcher);
                scPage = scService.findAll(example, pageable);
            }
            /**
             * 如果下拉框选中的是姓名
             */
            else {
                sc.setSname(content);
                sc.setTno(tno);
                ExampleMatcher matcher = ExampleMatcher.matching().
                        withMatcher("sname", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withMatcher("tno", ExampleMatcher.GenericPropertyMatchers.contains());
                Example<Sc> example = Example.of(sc, matcher);
                scPage = scService.findAll(example, pageable);
            }
        }



        PageUtils pageUtils = new PageUtils(scPage.getContent(),scPage.getTotalElements());
        return RUtil.success(pageUtils);


    }

    
}
